package club.hicode.daydayup.sslFile.client;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.LastHttpContent;
import io.netty.handler.stream.ChunkedFile;
import scala.collection.script.Start;

import java.io.File;
import java.io.FileInputStream;
import java.io.RandomAccessFile;

public class FileControl {

	private ChannelHandlerContext channel;

	public FileControl(ChannelHandlerContext channel) {
		this.channel = channel;
	}

	public void sendFile(String fileUrl) {
		RandomAccessFile raf = null;
		ByteBuf buffer = null;
		try {
			long start = System.currentTimeMillis();
			raf = new RandomAccessFile(fileUrl, "rw");
			ChunkedFile chunkedFile = new ChunkedFile(raf);
			System.out.println("FileLength:" + chunkedFile.length());


			channel.writeAndFlush(chunkedFile).addListener(new ChannelFutureListener() {
				@Override
				public void operationComplete(ChannelFuture future) throws Exception {
					System.out.println("operationComplete");
					future.channel().close();
					long end = System.currentTimeMillis();
					System.out.println(end - start);
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
